﻿using System;
using System.Linq;
using System.Printing;
using System.Windows;
using System.Windows.Input;
using AdonisUI.Controls;
using DBBaseServer.AbstractModel;
using YJTTool.Common.Events;
using YJTTool.MainWindow.Views;

namespace YJTTools.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly AbstractUserInfoServer abstractUserInfo;
        public MainWindow()
        {
            InitializeComponent();
            abstractUserInfo = new AbstractUserInfoServer();
        }
        private void textUser_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtUser.Focus();
            
        }
        private void txtUser_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUser.Text) && txtUser.Text.Length > 0)
            {
                textUser.Visibility = Visibility.Collapsed;
            }
            else
            {
                textUser.Visibility = Visibility.Visible;
            }
        }

        private void textPassword_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtPassword.Focus();
        }

        private void Border_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }

        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void txtPassword_TextChanged(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPassword.Password) && txtPassword.Password.Length > 0)
            {
                textPassword.Visibility = Visibility.Collapsed;
            }
            else
            {
                textPassword.Visibility = Visibility.Visible;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var user = abstractUserInfo.Query();

            var count = user.Where(s => s.Name == txtUser.Text && s.PassWord == txtPassword.Password).Count();

            if (count > 0)
            {
                
                var windowB = new NavigatePageView();
                windowB.Show();
                this.Close();
                BoolEvent.Instance.PushMessage(true);
            }
            else
            {
                
                var windowB = new NavigatePageView();
                windowB.Show();
                this.Close();
                BoolEvent.Instance.PushMessage(false);
            }
        }

        private void Button_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {

                var user = abstractUserInfo.Query();

                var count = user.Where(s => s.Name == txtUser.Text && s.PassWord == txtPassword.Password).Count();

                if (count > 0)
                {

                    var windowB = new NavigatePageView();
                    windowB.Show();
                    this.Close();
                    BoolEvent.Instance.PushMessage(true);
                }
                else
                {

                    var windowB = new NavigatePageView();
                    windowB.Show();
                    this.Close();
                    BoolEvent.Instance.PushMessage(false);
                }
            }
        }


    }
}
